<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <title>Document</title>
</head>

<body>
    <div id="app">
        <table border="box">
            <thead>
                <td>全选<input type="checkbox" :checked="check" @change="checkAll"/></td>
                <td>名字</td>
                <td>价格</td>
                <td>数量</td>
                <td>总价</td>
                <td colspan="2">操作</td>
            </thead>
            <tbody>
                <tr v-for="(obj,index) in goods">
                    <td><input type="checkbox" :checked="check"/></td>
                    <td><span v-if="obj.editF">{{obj.goodsName}}</span>
                        <input v-else type="text" v-model="obj.goodsName"/>
                    </td>
                    <td>{{obj.price}}</td>
                    <td>
                        <input type="button" value="-1" @click="minus(obj)">
                        {{obj.count}}
                        <button @click="plus(obj)"> +1 </button>
                    </td>
                    <td>{{obj.count * obj.price}}</td>
                    <td>
                        <button @click="edit(obj,index)"> 编辑 </button>
                    </td>
                    <td>
                        <button @click="deleteGood(index)"> 删除 </button>
                    </td>
                </tr>
                <tr >
                    <td colspan="4">合计</td>
                    <td >{{totalPrice}}</td>
                </tr>
            </tbody>


        </table>
    </div>
</body>

<script>
    const vm = new Vue({
        el: "#app",
        data: {
            check:false,
            goods: [
                {
                    goodsName: 'java',
                    price: 50,
                    count: 5,
                    editF:true
                },
                {
                    goodsName: 'python',
                    price: 20,
                    count: 10,
                    editF:true
                },
                {
                    goodsName: 'c++',
                    price: 100,
                    count: 1,
                    editF:true
                }
            ]
        },
        methods: {
            minus(obj) {
                if (obj.count > 0) {
                    obj.count--;
                } else {
                    alert('不能在点啦!')
                }
            },
            plus(obj) {
                obj.count++;
            },
            checkAll(event) {
                this.check = !this.check
            },
            deleteGood(index){
                this.goods.splice(index,1);
            },
            edit(obj,index){
                obj.editF = !obj.editF;
            }
        },
        computed:{
            totalPrice(){
                let total = 0;
                for(index in this.goods){
                    total += this.goods[index].price * this.goods[index].count
                }
                return total;
            }
        }
    });

</script>

</html>